/*
 * $Id: $
 */
package org.rcgwt.client.ui.grid.internal;

import org.rcgwt.client.ui.grid.internal.model.IElementModel;
import org.rcgwt.client.ui.grid.internal.model.IElementModelDelta;
import org.rcgwt.client.ui.grid.internal.model.IHeaderModel;
import org.rcgwt.client.ui.grid.internal.model.ITitleRowModel;
import org.rcgwt.client.ui.grid.internal.model.IViewModel;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class HeaderModel extends AbstractElementModel implements IHeaderModel {

	private ITitleRowModel mainTitleRowModel;

	private ITitleRowModel titleRowModels[];

	private final IViewModel viewModel;

	private final Position position;

	private int scrollPosition;

	protected HeaderModel(IViewModel viewModel, int modelIndex,
			Position position) {

		this.modelIndex = modelIndex;
		this.viewModel = viewModel;
		this.position = position;
	}

	@Override
	public Type getType() {
		return Type.HEADER;
	}

	public ITitleRowModel getMainTitleRowModels() {
		return mainTitleRowModel;
	}

	public void setTitleRowModels(ITitleRowModel mainTitleRowModel,
			ITitleRowModel titleRowModels[]) {
		this.mainTitleRowModel = mainTitleRowModel;
		this.titleRowModels = titleRowModels;
	}

	@Override
	public ITitleRowModel[] listTitleRowModels() {
		return titleRowModels;
	}

	@Override
	public IViewModel getViewModel() {
		return viewModel;
	}

	@Override
	public Orientation getOrientation() {
		switch (getPosition()) {
		case LEFT:
		case RIGHT:
			return Orientation.VERTICAL;

		default:
			return Orientation.HORIZONTAL;
		}
	}

	@Override
	public Position getPosition() {
		return position;
	}

	@Override
	public IElementModel getParent() {
		return getViewModel();
	}

	@Override
	public boolean setScrollPosition(int newScrollPosition) {
		if (scrollPosition == newScrollPosition) {
			return false;
		}

		((HeaderModelDelta) getModelElementDelta())
				.performScrollPositionChanged(scrollPosition, newScrollPosition);

		newScrollPosition = scrollPosition;

		return true;
	}

	@Override
	public IElementModelDelta<?> createDelta() {
		return new HeaderModelDelta(this);
	}

}
